package me.caofeng.pojo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import me.caofeng.enums.OrderStatus;
import me.caofeng.serializer.JsonDateSerializer;
import me.caofeng.util.UUIDUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Copyright © 2017曹峰. All rights reserved.
 *
 * @Prject: mall
 * @Package: me.caofeng.pojo
 * @Description:
 * @Author: 曹峰 blog.caofeng.me
 * @Date: 2017-06-29 11:54
 * @Version: V1.0
 */
public class Order extends Pojo {
    /**
     * 交易流水号
     */
    private String orderId;

    /**
     * 用户id
     */
    private String userId;

    /**
     * 用户地址id
     */
    private int addressId;

    /**
     * 地址信息
     */
    private Address address;

    /**
     * 快递信息
     */
    private PostInfo postInfo;

    /**
     * 最终支付价格,单位分
     */
    private int payPrice;
    /**
     * 字符方式
     */
    private int payType;
    /**
     * 邮费
     */
    private int postage;
    /**
     * 订单状态
     */
    private OrderStatus status;

    /**
     * 支付时间
     */
    @JsonSerialize(using = JsonDateSerializer.class)
    private Date payTime;
    /**
     * 发货时间
     */
    @JsonSerialize(using = JsonDateSerializer.class)
    private Date sendTime;
    /**
     * 交易完成时间
     */
    @JsonSerialize(using = JsonDateSerializer.class)
    private Date endTime;
    /**
     * 交易关闭时间
     */
    @JsonSerialize(using = JsonDateSerializer.class)
    private Date closeTime;

    /**
     * 交易关闭原因
     */
    private String closeReason;

    /**
     * 商品明细列表
     */
    private List<OrderItem> items;

    public Order() {
    }

    public Order(AlipayInfo alipayInfo) {
        setUserId(alipayInfo.getUserId());
        setOrderId(alipayInfo.getOrderId());
        setStatus(alipayInfo.getOrderStatus());
        setPayTime(alipayInfo.getPayTime());
        setCloseTime(alipayInfo.getCloseTime());
        setPayType(1);
    }

    public Order(Carts carts, String userId, int addressId) {
        this.userId = userId;
        this.orderId = UUIDUtil.generateOrderId();
        this.addressId = addressId;
        this.payPrice = carts.getCheckedPrice();
        this.postage = 0;
        this.status = OrderStatus.UNPAID;
        List<OrderItem> orderItems = new ArrayList<>();
        carts.getCartItems().forEach(cartItem -> {
            if (cartItem.getChecked().isStatus()) {
                OrderItem orderItem = new OrderItem();
                Product product = cartItem.getProduct();
                orderItem.setOrderId(this.orderId);
                orderItem.setUserId(userId);
                orderItem.setProductId(cartItem.getProductId());
                orderItem.setProductName(product.getName());
                orderItem.setProductImage(product.getMainImage());
                orderItem.setProductPrice(product.getPrice());
                orderItem.setQuantity(cartItem.getQuantity());
                orderItem.setTotalPrice(cartItem.getPrice());
                orderItems.add(orderItem);
            }

        });
        this.items = orderItems;
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public int getAddressId() {
        return addressId;
    }

    public void setAddressId(int addressId) {
        this.addressId = addressId;
    }

    public int getPayPrice() {
        return payPrice;
    }

    public void setPayPrice(int payPrice) {
        this.payPrice = payPrice;
    }

    public int getPayType() {
        return payType;
    }

    public void setPayType(int payType) {
        this.payType = payType;
    }

    public int getPostage() {
        return postage;
    }

    public void setPostage(int postage) {
        this.postage = postage;
    }

    public OrderStatus getStatus() {
        return status;
    }

    public void setStatus(OrderStatus status) {
        this.status = status;
    }

    public Date getPayTime() {
        return payTime;
    }

    public void setPayTime(Date payTime) {
        this.payTime = payTime;
    }

    public Date getSendTime() {
        return sendTime;
    }

    public void setSendTime(Date sendTime) {
        this.sendTime = sendTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public Date getCloseTime() {
        return closeTime;
    }

    public void setCloseTime(Date closeTime) {
        this.closeTime = closeTime;
    }

    public List<OrderItem> getItems() {
        return items;
    }

    public void setItems(List<OrderItem> items) {
        this.items = items;
    }

    public String getCloseReason() {
        return closeReason;
    }

    public void setCloseReason(String closeReason) {
        this.closeReason = closeReason;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public PostInfo getPostInfo() {
        return postInfo;
    }

    public void setPostInfo(PostInfo postInfo) {
        this.postInfo = postInfo;
    }

    @Override
    public String toString() {
        return "Order{" +
                "orderId=" + orderId +
                ", userId='" + userId + '\'' +
                ", addressId=" + addressId +
                ", payPrice=" + payPrice +
                ", id=" + id +
                ", createTime=" + createTime +
                ", payType=" + payType +
                ", updateTime=" + updateTime +
                ", isDelete=" + isDelete +
                ", postage=" + postage +
                ", status=" + status +
                ", payTime=" + payTime +
                ", sendTime=" + sendTime +
                ", endTime=" + endTime +
                ", closeTime=" + closeTime +
                '}';
    }


    /**
     * @Author:曹峰 blog.caofeng.me
     * @Date: 2017/7/3 22:38
     * @Description: 是否发货
     */
    @JsonIgnore
    public boolean isPost() {
        if (null == status) {
            return false;
        }
        switch (status) {
            case SEND:
            case END:
            case FINISHED:
                return true;
            default:
                return false;
        }
    }

    public static void main(String[] args) {
        Order order = new Order();
        order.setStatus(OrderStatus.SEND);
        System.out.println(order.isPost());
    }
}
